package com.dormitory.activity;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import com.dormitory.R;
import com.dormitory.application.MyApplication;
import com.dormitory.bean.DormitoryBean;
import com.dormitory.bean.UserBean;
import com.dormitory.controller.DormitoryController;
import com.dormitory.controller.NewsController;

/**
 * Created by 袁申 on 2017/10/22.
 */

public class AddNewsActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_news);
        ActionBar actionBar = this.getSupportActionBar();
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowHomeEnabled(false);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.add_notification_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_confirm:
                UserBean userBean = ((MyApplication)getApplication()).getUser();
                DormitoryBean dormitoryBean = ((MyApplication)getApplication()).getDormitory();
                if(userBean != null && dormitoryBean != null) {
                    AddNewsActivity.AddNewsAsyncTask addnewsAsyncTask = new AddNewsActivity.AddNewsAsyncTask();
                    addnewsAsyncTask.execute(((TextView)findViewById(R.id.textView6)).getText().toString(), dormitoryBean.getName()+":"+userBean.getName());
                }
                return true;
            case android.R.id.home:
                this.finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);

        }
    }
    private class AddNewsAsyncTask extends AsyncTask<String, Object, Integer> {

        private UserBean user;
        ProgressDialog progressDialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = new ProgressDialog(AddNewsActivity.this);
            progressDialog.setTitle("上传中");
            progressDialog.setIndeterminate(true);
            progressDialog.setCancelable(false);
            progressDialog.show();
        }
        @Override
        protected Integer doInBackground(String[] params) {
            NewsController newsController = NewsController.getInstance();
            return newsController.addnews(params[0], params[1]);
        }

        @Override
        protected void onPostExecute(Integer code) {
            super.onPostExecute(code);
            if(progressDialog != null && progressDialog.isShowing()) {
                progressDialog.cancel();
            }
            switch(code) {
                case 0:
                    Handler handler = ((MyApplication)getApplication()).getUpdateNewsHandler();
                    if(handler != null) {
                        handler.sendEmptyMessage(0x101);
                    }
                    finish();
                    break;
                default:
                    Toast.makeText(getApplicationContext(), "网络错误", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
